package com.tledu.controller;

import com.tledu.core.AjaxResult;
import com.tledu.model.Product;
import com.tledu.model.Work;
import com.tledu.service.ProductService;
import com.tledu.service.WorkService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("work")
@Controller
@AllArgsConstructor
public class WorkController {
    @Resource
    private WorkService workService;




    @GetMapping("/list")
    public String wor(ModelMap modelMap) {
        List<Work> worksList = workService.list();
        modelMap.addAttribute("worksList",worksList);
        return "work/work-list";
    }


    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult<Work> delete(String id){
        int i = workService.delete(id);
        if (i != 1){
            return new AjaxResult<Work>(false,"删除失败");
        }
        return new AjaxResult<Work>(true,"删除成功");
    }


    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<Work> insert(Work work){
        int i = workService.insert(work);
        System.out.println(work);
        if (i != 1){
            return new AjaxResult<Work>(false,"添加失败");
        }
        return new AjaxResult<Work>(true,"添加成功");
    }

    @GetMapping("/add")
    public String toAddJsp(){
        return "work/work-add";
    }



    @GetMapping("/edit")
    public String toEditJsp(String id,ModelMap modelMap){
        Work work= workService.selectById(id);
        modelMap.addAttribute("work",work);
        return "work/work-edit";
    }



    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<Work> update(@RequestBody  Work work){
        System.out.println(work);
        int i = workService.update(work);

        if (i != 1){
            return new AjaxResult<Work>(false,"修改失败,不能修改编号");
        }
        return new AjaxResult<Work>(true,"修改成功");
    }



}
